﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;


namespace Web.Admin.ashx
{
    /// <summary>
    /// feditor 的摘要说明
    /// </summary>
    public class feditor : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private string filePath = Evan.Common.CommFun.GetAppSetting("UploadFolder"); //上传目录名

        public void ProcessRequest(HttpContext context)
        {
            //检查管理员是否登录
            if (!Evan.Member.Administrator.IsAdminLogin())
            {
                context.Response.StatusCode = 404;
                context.Response.End();
            }

            string action = Evan.Common.CommFun.GetQueryString("action");
            switch (action)
            {
                case "singleuploadimage": //单图上传
                    Evan.Plugins.Upload.fileItemModel imageItemModel = Evan.Plugins.Upload.UploadProcess(context);
                    context.Response.Write(Evan.Plugins.Upload.ConvertUploadResult(imageItemModel));
                    //context.Response.Write("{\"link\":\"" + imageItemModel.url + "\",\"msg\":\"" + imageItemModel.error + "\"}");
                    break;
                case "deleteimage": //删除图片
                    #region 图片删除方法
                    string imagePath = Evan.Common.CommFun.GetForm("src");
                    if (!string.IsNullOrEmpty(imagePath))
                    {
                        //检查路径前几个字节
                        if (imagePath.StartsWith("/" + filePath + "/"))
                        {
                            if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(imagePath)))
                            {
                                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(imagePath));
                            }
                            else
                            {
                                throw new Exception("图片不存在");
                            }
                        }
                        else
                        {
                            throw new Exception("非法请求");
                        }
                    }
                    else
                    {
                        throw new Exception("路径为空");
                    }
                    #endregion
                    break;
                case "loadimagelist": //获取图片列表
                    List<Evan.Model.SysModel.FEditorImageInfo> fileinfo = getFile(HttpContext.Current.Server.MapPath("/uploads/"), ".jpg.png.gif.jpeg.bmp", null);
                    string res = Evan.Common.JsonHelper.ObjectToJSON(fileinfo);
                    context.Response.Write(res);
                    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\"}]");
                    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\",\"name\":\"Image 2019-07-15 at 22:07:24.jpg\",\"type\":\"folder\",\"id\":1,\"tag\":\"beach\"}]");
                    break;
                case "singleuploadfile": //上传附件
                    Evan.Plugins.Upload.fileItemModel fileItemModel = Evan.Plugins.Upload.UploadProcess(context);
                    context.Response.Write(Evan.Plugins.Upload.ConvertUploadResult(fileItemModel));
                    //context.Response.Write("{\"link\":\"" + fileItemModel.url + "\",\"msg\":\"" + fileItemModel.error + "\"}");
                    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\",\"name\":\"Image 2019-07-15 at 22:07:24.jpg\",\"type\":\"image\",\"id\":1,\"tag\":\"beach\"}]");
                    break;
                //case "deletefile": //删除附件
                //    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\",\"name\":\"Image 2019-07-15 at 22:07:24.jpg\",\"type\":\"image\",\"id\":1,\"tag\":\"beach\"}]");
                //    break;
                //case "loadfilelist": //上传附件列表
                //    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\",\"name\":\"Image 2019-07-15 at 22:07:24.jpg\",\"type\":\"image\",\"id\":1,\"tag\":\"beach\"}]");
                //    break;
                case "singleuploadvideo": //上传视频
                    Evan.Plugins.Upload.fileItemModel videoItemModel = Evan.Plugins.Upload.UploadProcess(context);
                    context.Response.Write(Evan.Plugins.Upload.ConvertUploadResult(videoItemModel));
                    //context.Response.Write("{\"link\":\"" + videoItemModel.url + "\",\"msg\":\"" + videoItemModel.error + "\"}");
                    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\",\"name\":\"Image 2019-07-15 at 22:07:24.jpg\",\"type\":\"image\",\"id\":1,\"tag\":\"beach\"}]");
                    break;
                //case "deletevideo": //删除附件
                //    //context.Response.Write("[{\"url\":\"/uploads/20190829120259_162514.png\",\"thumb\":\"/uploads/20190829120259_162514.png\",\"name\":\"Image 2019-07-15 at 22:07:24.jpg\",\"type\":\"image\",\"id\":1,\"tag\":\"beach\"}]");
                //    break;
                //case "loadvideolist": //上传附件列表
                //    break;
                default:
                    break;
            }
        }


        /// <summary>
        /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
        /// </summary>
        /// <param name="path">文件夹路径 相对站点根目录</param>
        /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
        /// <returns>List<FileInfo></returns>
        public static List<Evan.Model.SysModel.FEditorImageInfo> getFile(string path, string extName, List<Evan.Model.SysModel.FEditorImageInfo> list)
        {
            try
            {
                //path = HttpContext.Current.Server.MapPath(path);
                if (list == null)
                {
                    list = new List<Evan.Model.SysModel.FEditorImageInfo>();
                }
                //List<Evan.Model.SysModel.FEditorImageInfo> lst = new List<Evan.Model.SysModel.FEditorImageInfo>();
                string[] dir = Directory.GetDirectories(path); //文件夹列表   
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] file = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件   
                    {
                        if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                        {
                            list.Add(new Evan.Model.SysModel.FEditorImageInfo() { url = (f.FullName.Replace(HttpContext.Current.Server.MapPath("/").TrimEnd('\\'), "").Replace("\\","/")), thumb = (f.FullName.Replace(HttpContext.Current.Server.MapPath("/").TrimEnd('\\'), "").Replace("\\", "/")), tag = (f.DirectoryName.Substring(f.Directory.ToString().LastIndexOf('\\') + 1)), name = f.Name });
                        }
                    }
                    foreach (string d in dir)
                    {
                        getFile(d, extName, list);//递归   
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}